//
//  ChooseContactPersonView.m
//  HuaRun2
//
//  Created by admin on 17/9/1.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "ChooseContactPersonView.h"
#import "BaseDynamicTagView.h"
#import "ImageLabView.h"

@interface ChooseContactPersonView ()<UIGestureRecognizerDelegate>
@property (nonatomic, strong)UIView * backView;

//返回按钮底 视图
@property (nonatomic, strong) UIView * noteBgView;
//重置按钮底 视图
@property (nonatomic, strong) UIView * resetBgView;
//确定按钮底 视图
@property (nonatomic, strong) UIView * sureBgView;

@end

@implementation ChooseContactPersonView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
#pragma mark - life cycle
- (void)dealloc {
    NSLog(@"%@ dealloc", [self class]);
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self viewInit];
        [self layoutInit];
    }
    return self;
}

- (void)viewInit {
    self.backgroundColor = RGBA(149, 149, 149, 0.5);
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissSelfMethod)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];

    self.backView = [[UIView alloc] initWithFrame:(CGRectMake(CGRectGetMaxX(self.frame),
                                                              0,
                                                              kChooseViewWidth,
                                                              self.xj_height))];
    _backView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.backView];
    
    self.scrollBackView = [UIScrollView new];
    _scrollBackView.showsVerticalScrollIndicator = NO;
    _scrollBackView.showsHorizontalScrollIndicator = NO;
    [_backView addSubview:_scrollBackView];

    [_scrollBackView addSubview:self.tagLabel];
    
    self.tagView = [[BaseDynamicTagView alloc] initWithFrame:CGRectMake(0,
                                                                        CGRectGetMaxY(_tagLabel.frame),
                                                                        CGRectGetWidth(self.backView.frame),
                                                                        100)];
    [_scrollBackView addSubview:_tagView];
    
    [_scrollBackView addSubview:self.departmentLabel];

    self.buMenTagView = [[BaseDynamicTagView alloc] initWithFrame:CGRectMake(self.tagView.xj_x,                                                                   200,
                                                                             _tagView.xj_width,
                                                                             _tagView.xj_height)];
//    _buMenTagView.backgroundColor = [UIColor redColor];
//    _buMenTagView.hidden = YES;
    [_scrollBackView addSubview:_buMenTagView];
    
    self.noteBgView = [self makeCommonViewBackColor: RGBA(249, 249, 249, 1)];
    [_backView addSubview:_noteBgView];
    [_backView addSubview:self.noteView];
    
    self.resetBgView = [self makeCommonViewBackColor:[UIColor whiteColor]];
    [_backView addSubview:_resetBgView];
    [_backView addSubview:self.resetView];
    
    self.sureBgView = [self makeCommonViewBackColor:RGBA(243, 161, 50, 1)];
    [_backView addSubview:_sureBgView];
    [_backView addSubview:self.sureView];


}


-(int)getRandomNumber:(int)from to:(int)to
{
    return (int)(from + (arc4random() % (to - from + 1)));
}

- (void)layoutInit {
    
    weakself(ws);

    [self.scrollBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(ws.backView);
        make.top.mas_equalTo(ws.backView);
        make.bottom.mas_equalTo(ws.noteView.mas_top);
    }];
    
    [self.noteBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(ws.backView);
        make.left.mas_equalTo(ws.backView).offset(0);
        make.right.mas_equalTo(ws.resetBgView.mas_left);
        make.height.mas_equalTo(ws.sureBgView);
    }];
    
    [self.noteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(ws.backView.mas_bottom).offset(-5);
        make.left.mas_equalTo(ws.backView).offset(kMargin10);
    }];

    [self.resetBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(ws.backView);
        make.centerX.mas_equalTo(ws.backView).offset(0);
        make.width.mas_equalTo(ws.backView).multipliedBy(0.34);
        make.height.mas_equalTo(ws.sureBgView);
    }];

    [self.resetView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(ws.noteView);
        make.centerX.mas_equalTo(ws.backView);
    }];

    [self.sureBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(ws.backView);
        make.right.mas_equalTo(ws.backView);
        make.width.mas_equalTo(ws.backView).multipliedBy(0.33);
        make.height.mas_equalTo(40);
    }];
    
    [self.sureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(ws.noteView);
        make.right.mas_equalTo(ws.backView).offset(-kMargin10);
    }];

    [UIView animateWithDuration:0.3 animations:^{
        ws.backView.frame = CGRectMake(CGRectGetMaxX(self.frame) - kChooseViewWidth, 0, kChooseViewWidth, self.xj_height);
    }];

}


- (void)dismissSelfMethod {
    
    weakself(ws);
    
    [self.tagView resetSelectedConditional];
    [self.buMenTagView resetSelectedConditional];

    [UIView animateWithDuration:0.3 animations:^{

        ws.backView.frame = CGRectMake(CGRectGetMaxX(self.frame), 0, kChooseViewWidth, self.xj_height);
    } completion:^(BOOL finished) {
        
        [ws.backView removeFromSuperview];
        [ws removeFromSuperview];
    }];

}

//是否允许响应手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
//    当点击backview时不响应手势
    if ([touch.view isDescendantOfView:self.backView]) {
        return NO;
    }
    
    return YES;
    
}
//制作一个通用的view， 占backView的宽度 1/3
//放到  应用登  3个视图的底部，不想再封装了
- (UIView *)makeCommonViewBackColor:(UIColor *)color {
    UIView * view = [UIView new];
    view.backgroundColor = color;
    return view;
}

#pragma mark getter
- (ImageLabView *)noteView {
    if (!_noteView) {
        self.noteView = [ImageLabView new];
        _noteView.iconView.image = [UIImage imageNamed:@"返回"];
        _noteView.infoStr = NSLocalizedString(@"返回",nil);
        _noteView.nameLab.textColor = [UIColor blackColor];
        _noteView.backgroundColor = [UIColor clearColor];
    }
    return _noteView;
}
- (ImageLabView *)resetView {
    if (!_resetView) {
        self.resetView = [ImageLabView new];
        _resetView.iconView.image = [UIImage imageNamed:@"重置"];
        _resetView.infoStr = NSLocalizedString(@"重置",nil);
        _resetView.nameLab.textColor = RGBA(243, 161, 50, 1);
        _resetView.backgroundColor = [UIColor clearColor];
    }
    return _resetView;
}

- (ImageLabView *)sureView {
    if (!_sureView) {
        self.sureView = [ImageLabView new];
        _sureView.iconView.image = [UIImage imageNamed:@"应用"];
        _sureView.infoStr = NSLocalizedString(@"应用", nil);
        _sureView.backgroundColor = [UIColor clearColor];
        _sureView.nameLab.textColor = [UIColor whiteColor];
    }
    return _sureView;
}

- (UILabel *)tagLabel {
    if (!_tagLabel) {
        self.tagLabel = [UILabel new];
        _tagLabel.font = kFontSize15;// [UIFont boldSystemFontOfSize:15];
        _tagLabel.textColor = [UIColor blackColor];
        _tagLabel.frame = CGRectMake(kMargin10, kMargin15 * 2, 100, 20);
        _tagLabel.text = NSLocalizedString(@"call", nil);
//        @"称谓";
    }
    return _tagLabel;
}

- (UILabel *)departmentLabel {
    if (!_departmentLabel) {
        self.departmentLabel = [UILabel new];
        _departmentLabel.font = kFontSize15;// [UIFont boldSystemFontOfSize:15];
        _departmentLabel.textColor = [UIColor blackColor];
        _departmentLabel.text = NSLocalizedString(@"部门标签",nil);
    }
    return _departmentLabel;
}

@end
